package com.sj.purchase.modules.purchase.web;

import com.sj.purchase.common.utils.FileOperateUtil;
import com.sj.purchase.modules.purchase.domain.Purchase;
import com.sj.purchase.modules.purchase.service.PurchaseService;
import com.sj.purchase.modules.purchase.views.VPurchase;
import com.sj.purchase.util.excel.ExcelUtil;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by icodebug on 17-5-31.
 */
@Controller
@RequestMapping("/purchase")
public class PurchaseController {

    @Autowired
    private PurchaseService purchaseService;


    @PostMapping(value = "/save")
    public ResponseEntity save(@RequestBody Set<Purchase> purchases) {

        purchaseService.save(purchases);

        return ResponseEntity.ok("ok");
    }


    @RequestMapping(method = RequestMethod.GET)
    public String get(@RequestParam(value = "startDate", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date startDate,
                      @PageableDefault(value = 20, direction = Sort.Direction.DESC) Pageable page, Model model) {
        try {
            startDate = startDate == null ? new Date() : startDate;
            Map<String, Object> purchaseMap = purchaseService.getByDate(startDate, page);

            model.addAttribute("purchase", purchaseMap);
            model.addAttribute("startDate", DateFormatUtils.format(startDate, "yyyy-MM-dd"));

        } catch (Exception e) {
            e.printStackTrace();
        }

        return "purchase";

    }


    @GetMapping("/json")
    @ResponseBody
    public Map getJSON(@RequestParam(value = "startDate", required = false) Date startDate,
                       @PageableDefault(value = 20, direction = Sort.Direction.DESC) Pageable page) {

        startDate = startDate == null ? new Date() : startDate;
        return purchaseService.getByDate(startDate, page);
    }

    //下载数据
    @GetMapping(value = "/excel")
    public void get(@RequestParam(value = "startDate", required = false, defaultValue = "2016-01-01")  Date startDate, HttpServletRequest request, HttpServletResponse response) throws Exception {

        try {
            Map<String, Object> purchaseMap = purchaseService.getByDate(startDate, null);
            List<VPurchase> vPurchases = ((List<VPurchase>) purchaseMap.get("content"));

            FileOperateUtil.download(request, response, DateFormatUtils.format(startDate, "yyyy-MM-dd") + " CaiGou.xls", new ExcelUtil<VPurchase>(VPurchase.class).exportExcel(vPurchases, "订单信息"));

        } catch (Exception e) {
            e.printStackTrace();
        }

    }


}
